package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.utils.StringUtils;
import com.distribution.system.domain.entity.AddressSystem;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.entity.CmccDaChildCard;
import com.distribution.system.domain.req.ChildCardInfoReq;
import com.distribution.system.domain.req.CmccDaProductListReq;
import com.distribution.system.domain.req.CmccDaChildCardAddReq;
import com.distribution.system.mapper.CmccDaChildCardMapper;
import com.distribution.system.service.AddressSystemService;
import com.distribution.system.service.CmccDaProductService;
import com.distribution.system.service.CmccDaChildCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: GodLu
 * @create: 2024-04-10 16:20
 * @description: TODO
 */
@Service("CmccDaChildCardService")
public class CmccDaChildCardServiceImpl extends ServiceImpl<CmccDaChildCardMapper, CmccDaChildCard> implements CmccDaChildCardService {
    @Resource
    CmccDaProductService cmccDaProductService;
    @Resource
    AddressSystemService addressSystemService;

    @Override
    public List<CmccDaChildCard> childCardList(CmccDaProductListReq cmccDaProductListReq) {
        //System.out.println("cmccDaProductListReq = " + cmccDaProductListReq);
        LambdaQueryWrapper<CmccDaChildCard> cmccDaChildCardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cmccDaChildCardLambdaQueryWrapper
                .like(!StringUtils.isEmpty(cmccDaProductListReq.getProductName()), CmccDaChildCard::getProductName, cmccDaProductListReq.getProductName())
                .eq(!Objects.isNull(cmccDaProductListReq.getProductTypeId()), CmccDaChildCard::getProductTypeId, cmccDaProductListReq.getProductTypeId())
                .eq(!Objects.isNull(cmccDaProductListReq.getProductStatus()), CmccDaChildCard::getProductStatus, cmccDaProductListReq.getProductStatus())
                .eq(!StringUtils.isNull(cmccDaProductListReq.getProductCode()), CmccDaChildCard::getParentCode, cmccDaProductListReq.getProductCode());
        cmccDaChildCardLambdaQueryWrapper.orderByAsc(CmccDaChildCard::getSort);
        return list(cmccDaChildCardLambdaQueryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveChildCard(CmccDaChildCardAddReq cmccDaChildCardAddReq) {
        List<CmccDaChildCard> cmccDaChildCardList = new ArrayList<>();
        //查询父卡
        CmccDaProduct parentCard = cmccDaProductService.getByProductCode(cmccDaChildCardAddReq.getParentCode());
        //获取分卡基本信息集合
        List<ChildCardInfoReq> cardInfoList = cmccDaChildCardAddReq.getProductInfoList();
        for (ChildCardInfoReq childCardInfoReq : cardInfoList) {
            CmccDaChildCard cmccDaChildCard = new CmccDaChildCard();
            //设置获取到的属性
            cmccDaChildCard.setProductParam(childCardInfoReq.getProductParam());
            cmccDaChildCard.setRegularProvinceCode(childCardInfoReq.getRegularProvinceCode());
            cmccDaChildCard.setRegularCityCode(childCardInfoReq.getRegularCityCode());
            String provinceNameByCode = addressSystemService.getProvinceNameByCode(childCardInfoReq.getRegularProvinceCode());
            String cityNameByCode = addressSystemService.getCityNameByCode(childCardInfoReq.getRegularCityCode());
            cmccDaChildCard.setRegularProvince(provinceNameByCode);
            cmccDaChildCard.setRegularCity(cityNameByCode);

            cmccDaChildCard.setParentCode(cmccDaChildCardAddReq.getParentCode());
            cmccDaChildCard.setProductName(cmccDaChildCardAddReq.getProductName());
            cmccDaChildCard.setProductStatus(cmccDaChildCardAddReq.getProductStatus());
            cmccDaChildCard.setBindChannelInfoId(cmccDaChildCardAddReq.getBindChannelInfoId());
            List<CmccDaChildCard> list = list();
            cmccDaChildCard.setSort(Objects.isNull(cmccDaChildCardAddReq.getSort()) ? 1 : cmccDaChildCardAddReq.getSort());
            //排序有重复值
            List<CmccDaChildCard> collect = list.stream().filter(ff -> ff.getSort().equals(cmccDaChildCard.getSort())).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                List<CmccDaChildCard> collect1 = list.stream().filter(dd -> dd.getSort() >= cmccDaChildCard.getSort()).collect(Collectors.toList());
                if (!collect1.isEmpty()) {
                    collect1.forEach(gg->gg.setSort(gg.getSort() +1));
                    updateBatchById(collect1);
                }
            }
            if (StringUtils.isEmpty(cmccDaChildCardAddReq.getProductDesc())) {
                cmccDaChildCard.setProductDesc(cmccDaChildCard.getProductName() + "【" + cmccDaChildCard.getRegularProvince() + "】");
            }else {
                cmccDaChildCard.setProductDesc(cmccDaChildCardAddReq.getProductDesc());
            }
            //设置与父卡一致信息
            cmccDaChildCard.setSelectNumberFlag(parentCard.getSelectNumberFlag());
            cmccDaChildCard.setCodeFlag(parentCard.getCodeFlag());
            cmccDaChildCard.setBroadbandFlag(parentCard.getBroadbandFlag());
            cmccDaChildCard.setPlaceRegularFlag(parentCard.getPlaceRegularFlag());
            cmccDaChildCard.setProductTypeId(parentCard.getProductTypeId());
            cmccDaChildCard.setProfitConditionId(parentCard.getProfitConditionId());
            cmccDaChildCard.setProfitConditionDesc(parentCard.getProfitConditionDesc());
            cmccDaChildCard.setBasicsCommission(parentCard.getBasicsCommission());
            cmccDaChildCard.setCreateTime(LocalDateTime.now());
            cmccDaChildCard.setUpdateTime(LocalDateTime.now());
            //添加到集合
            cmccDaChildCardList.add(cmccDaChildCard);
        }
        //System.out.println("cmccDaChildCardList = " + cmccDaChildCardList);
        //保存
        saveBatch(cmccDaChildCardList);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateChildCard(CmccDaChildCard cmccDaChildCard) {
        LambdaQueryWrapper<CmccDaChildCard> childCardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        childCardLambdaQueryWrapper.ne(CmccDaChildCard::getId, cmccDaChildCard.getId());
        List<CmccDaChildCard> list = list(childCardLambdaQueryWrapper);
        //根据省市编码设置归属地省市名称
        if (StringUtils.isEmpty(cmccDaChildCard.getRegularProvinceCode())) {
            cmccDaChildCard.setRegularProvince(null);
        } else if (StringUtils.isEmpty(cmccDaChildCard.getRegularCityCode())) {
            cmccDaChildCard.setRegularCity(null);
            cmccDaChildCard.setRegularProvince(addressSystemService.getProvinceNameByCode(cmccDaChildCard.getRegularProvinceCode()));
        }else {
            cmccDaChildCard.setRegularProvince(addressSystemService.getProvinceNameByCode(cmccDaChildCard.getRegularProvinceCode()));
            cmccDaChildCard.setRegularCity(addressSystemService.getCityNameByCode(cmccDaChildCard.getRegularCityCode()));
        }
        //更新为null的值
        update(cmccDaChildCard,new LambdaUpdateWrapper<CmccDaChildCard>()
                .set(Objects.isNull(cmccDaChildCard.getRegularProvinceCode()),CmccDaChildCard::getRegularProvinceCode,null)
                .set(Objects.isNull(cmccDaChildCard.getRegularProvinceCode()),CmccDaChildCard::getRegularProvince,null)
                .set(Objects.isNull(cmccDaChildCard.getRegularCityCode()),CmccDaChildCard::getRegularCityCode,null)
                .set(Objects.isNull(cmccDaChildCard.getRegularCityCode()),CmccDaChildCard::getRegularCity,null)
                .eq(CmccDaChildCard::getId,cmccDaChildCard.getId()));
        //排序有重复值
        List<CmccDaChildCard> collect = list.stream().filter(ff -> ff.getSort().equals(cmccDaChildCard.getSort())).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            List<CmccDaChildCard> geCollect = list.stream().filter(dd -> dd.getSort() >= cmccDaChildCard.getSort()).collect(Collectors.toList());
            if (!geCollect.isEmpty()) {
                geCollect.forEach(gg->gg.setSort(gg.getSort() +1));
                updateBatchById(geCollect);
            }
        }
        //设置其他值
        cmccDaChildCard.setUpdateTime(LocalDateTime.now());
        updateById(cmccDaChildCard);
    }
}
